package cn.wei.sdd.core.service.impl;

import cn.wei.sdd.core.enums.ExamineStatusEnum;
import cn.wei.sdd.core.mapper.UserAccountMapper;
import cn.wei.sdd.core.mapper.UserMapper;
import cn.wei.sdd.core.pojo.entity.Examine;
import cn.wei.sdd.core.mapper.ExamineMapper;
import cn.wei.sdd.core.pojo.entity.User;
import cn.wei.sdd.core.pojo.entity.UserAccount;
import cn.wei.sdd.core.pojo.vo.ExamineVO;
import cn.wei.sdd.core.service.ExamineService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 回收员审核表 服务实现类
 * </p>
 *
 * @author Wei
 * @since 2021-10-22
 */
@Service
public class ExamineServiceImpl extends ServiceImpl<ExamineMapper, Examine> implements ExamineService {


    @Resource
    private UserMapper userMapper;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void SaveExamineVOByUserId(ExamineVO examineVO, Long userId) {

        Examine examine = new Examine();
        BeanUtils.copyProperties(examineVO,examine);
        examine.setUserId(userId);
        examine.setStatus(ExamineStatusEnum.AUTH_RUN.getStatus());
        baseMapper.insert(examine);
    }

    @Override
    public IPage<Examine> listPage(Page<Examine> pageParam, String keyword) {
        if (StringUtils.isBlank(keyword)){
            QueryWrapper<Examine> examineQueryWrapper = new QueryWrapper<>();
            examineQueryWrapper.orderByDesc("id");
            return baseMapper.selectPage(pageParam,examineQueryWrapper);
        }
        QueryWrapper<Examine> examineQueryWrapper = new QueryWrapper<>();
        examineQueryWrapper
                .like("user_name",keyword)
                .or().like("mobile",keyword)
        .orderByDesc("id");

        return baseMapper.selectPage(pageParam,examineQueryWrapper);
    }

    @Override
    public Integer getExamineStatus(Long userId) {
        QueryWrapper<Examine> examineQueryWrapper = new QueryWrapper<>();
        examineQueryWrapper.select("status").eq("user_id",userId);
        List<Object> objects = baseMapper.selectObjs(examineQueryWrapper);
        if (objects.size()==0){
            return ExamineStatusEnum.NO_AUTH.getStatus();
        }
        Integer status = (Integer) objects.get(0);

        return status;
    }

    @Override
    public void examine(Long examineId, Integer status, Long userId, BigDecimal getRate) {

        //获取审核信息
        QueryWrapper<Examine> examineQueryWrapper = new QueryWrapper<>();
        examineQueryWrapper.eq("id",examineId);
        Examine examine = baseMapper.selectOne(examineQueryWrapper);


        //更新审核
        Examine examineUpdate = new Examine();
        examineUpdate.setId(examineId);
        examineUpdate.setStatus(status);
        examineUpdate.setReviewer(userId);
        baseMapper.updateById(examineUpdate);


        //通过就创建会员账户
        if(ExamineStatusEnum.AUTH_OK.getStatus()==status){
            UserAccount userAccount = new UserAccount();
            userAccount.setUserId(examine.getUserId());
            userAccount.setUserType(Math.toIntExact(User.Recycler));
            userAccountMapper.insert(userAccount);

            //更新用户表
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",examine.getUserId())
                    .set("status",User.STATUS_NORMAL)
                    .set("examine_status",Examine.IS_APPROVED)
                    .set("percent",getRate);
            userMapper.update(null,updateWrapper);
        }

    }


}
